//
//  STianjiaViewController.m
//  News
//
//  Created by dlios on 15-5-13.
//  Copyright (c) 2015年 PEAK. All rights reserved.
//

#import "STianjiaViewController.h"
#import "SYueduyijiViewController.h"
#import "SNetWorkHandle.h"
#import "SRead.h"
#import "SReadModel.h"
#import "SReadFirstDetail.h"
#import "SReadyijiModel.h"
@interface STianjiaViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, assign) NSInteger i;

@property (nonatomic, retain) UITableView *tableView1;
@property (nonatomic, retain) UITableView *tableView2;

@property (nonatomic, retain) NSMutableArray *array;
@property (nonatomic, retain) SRead *read;

@property(nonatomic, retain) UIActivityIndicatorView *activityIndicator;

@property(nonatomic, retain) UILabel *label;


@property (nonatomic, assign) NSInteger cur;

@end

@implementation STianjiaViewController


- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.cur = 0;
    
    //请求出现小菊花
    UIImageView *view = [[UIImageView alloc] initWithFrame:self.view.bounds];
    view.image = [UIImage imageNamed:@"5EA2D64D-15BE-43D8-AB18-E15BCE039C8F"];
    view.tag = 1000;
    [view setBackgroundColor:[UIColor blackColor]];
    view.alpha = 1;
    [self.view addSubview:view];
    [view release];
    
    _activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 32.0f, 32.0f)];
    [_activityIndicator setCenter:view.center];
    [_activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];
    [view addSubview:_activityIndicator];
    
    [_activityIndicator startAnimating];
    
    
    
    self.navigationController.navigationBar.translucent = NO;
   //默认运行时点击哪一行
//    [self tableView:self.tableView1 didSelectRowAtIndexPath:0];
    self.array = [NSMutableArray array];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-fanhui"] style:UIBarButtonItemStylePlain target:self action:@selector(fanhui)];
    //[self jianlitableView];
    self.title = @"订阅";
    [self getData];
    
    
   
    
}

//建立tableView
- (void)jianlitableView
{
    self.tableView1 = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width / 3, self.view.frame.size.height) style:UITableViewStylePlain];
    _tableView1.delegate = self;
    _tableView1.dataSource = self;
    [self.view addSubview:_tableView1];
    //只能上啦 ,不能下拉
    _tableView1.bounces = NO;
    [_tableView1 release];
    self.tableView2 = [[UITableView alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 3, 0, 2*(self.view.frame.size.width / 3), self.view.frame.size.height) style:UITableViewStylePlain];
    _tableView2.delegate = self;
    _tableView2.dataSource = self;
    [self.view addSubview:_tableView2];
    //只能上啦 ,不能下拉
    _tableView2.bounces = NO;
    [_tableView2 release];

}

//获取数据
- (void)getData
{
    // 参数1: 网络接口
    // 参数2: 返回数据的Block 参数result中是网络请求的返回数据
    [SNetWorkHandle getDataWithURL:@"http://c.3g.163.com/nc/topicset/ios/v4/subscribe/read/all.html" completion:^(id result) {
        // result为请求接口的返回数据
        
        //NSLog(@"%@", result);
        
        //数组接收
        NSMutableArray *arr = result;
  
        for (NSDictionary *dic in arr) {
            SReadModel *read = [[SReadModel alloc] initWithDic:dic];
            for (NSDictionary *tListdic in read.tList) {
                SReadFirstDetail *detail = [[SReadFirstDetail alloc] initWithDic:tListdic];
                [read.tListArr addObject:detail];
                [detail release];
            }
            
            [self.array addObject:read];
            [read release];
        }
        
    
        
        [self jianlitableView];
        
    }];
    
}

//返回按钮点击方法
- (void)fanhui
{
    [self.navigationController popViewControllerAnimated:YES];
}
/**
 *  建立tableView
 */
/**
 *  实现协议方法
 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == _tableView1) {
        return self.array.count;
    } else if (tableView == _tableView2) {
        return [self.array[self.i] tListArr].count;
    }
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"str"];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"str"];
    }
    
    
    
    if (tableView == _tableView1) {
        if (self.cur == indexPath.row) {
            
           cell.backgroundColor = [UIColor grayColor];
        
        } else {
            
            cell.backgroundColor = [UIColor whiteColor];
            
        }
       
        cell.textLabel.text = [self.array[indexPath.row] cName];

       // cell.imageView.image = [UIImage imageNamed:@"iconfont-dingyue"];
        [cell.layer setBorderWidth:1];
        [cell.layer setCornerRadius:10];
        [cell.layer setBorderColor:[UIColor whiteColor].CGColor];
        
        //默认点击第0行
       //[self tableView:self.tableView1 didSelectRowAtIndexPath:0];
        
        return cell;
    } else if (tableView == _tableView2) {
        
        cell.textLabel.text = [[[self.array[self.i] tListArr] objectAtIndex:indexPath.row] tname];
    cell.detailTextLabel.text = [[[self.array[self.i] tListArr] objectAtIndex:indexPath.row] subnum];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        return cell;
    }
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (tableView == _tableView1){
        
        self.i = indexPath.row;
        self.cur = indexPath.row;
        
       // NSLog(@"%ld", self.cur);

        [_tableView1 reloadData];
        [_tableView2 reloadData];
        
}
    
    if (tableView == _tableView2) {
        
        
    SYueduyijiViewController *yiji = [[[SYueduyijiViewController alloc] init] autorelease];
        yiji.str = [[[self.array[self.i] tListArr] objectAtIndex:indexPath.row] tid];
        yiji.biaoti = [[[self.array[self.i] tListArr] objectAtIndex:indexPath.row] tname];
    [self.navigationController pushViewController:yiji animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
